package com.app.service.impl;

import com.app.dao.ShareDao;
import com.app.dao.UserDao;
import com.app.pojo.*;
import com.app.service.IUserService;
import com.app.utils.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;

@Service
public class UserService implements IUserService {


    private UserDao userDao;
    private ShareDao shareDao;

    public UserService(UserDao userDao, ShareDao shareDao) {
        this.userDao = userDao;
        this.shareDao = shareDao;
    }

    @Override
    public UserInfo getUserInfoById(String userId) {
        try {
            UserInfo userinfo = new UserInfo(userDao.finUserByName(userId));

            return userinfo;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public String loginByPhone(String email, String password) {
        return null;
    }

    @Override
    public String loginByEmail(String email,String password) {
        try {
            UserPassword upw=userDao.findUserPasswdByEmail(email);
            if (upw!=null&&upw.isPassword(password)) return upw.getUserId();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("email="+email);
            System.out.println("password="+password);
        }
        return null;
    }

    /**
     *
     * @param nickname 用户昵称
     * @param password 密码
     * @param email 邮箱
     * @return SUCCESS，USER_ALREADY_EXIST，SYSTEM_ERROR，
     */
    @Override
    public int register(String nickname, String password,String email) {
        //新建id
        String userId=""+new Date().getTime()+(int)(Math.random()*10000);
        UserSecretInfo user=new UserSecretInfo(userId,nickname,email,password);
        try {//会更改数据库，开启事务
            doRegisterTrans(user);
            return Status.SUCCESS;
        }catch (Exception e){
            String error=e.getMessage();
            System.out.println(error);
            if (error.equals("用户已存在")) return Status.USER_ALREADY_EXIST;
            return Status.SYSTEM_ERROR;
        }

    }
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=12000,rollbackFor=Exception.class)
    public void doRegisterTrans(User user){
        String error="";
        if (userDao.finUserByEmail(user.email)==null){
            //添加到数据库
            if(userDao.addUser(user)==1){
                //建立相关文件
                if (FileUtils.makeUserFiles(user.userId)) return ;
                else error="创建用户文件失败";
            }else error="数据库建表错误";
        }else error="用户已存在";
        throw new RuntimeException(error);
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=12000,rollbackFor=Exception.class)
    public boolean closeAccount(String userId) {
        //删除用户相关表
        if (userDao.deleteUser(userId)<1|| shareDao.delSharesById(userId)<1|| userDao.deleteLabelsById(userId)<1) {
            makeException("删除失败！");
            return false;
        } else{
            //删除文件
            FileUtils.removeUserFiles(userId);
            return true;
        }
    }
    private void makeException(String msg){
        throw new RuntimeException(msg);
    }
}
